package com.example.demo.serviceImpl.coreport;

import com.example.demo.dao.coreport.CoreportBugDao;
import com.example.demo.dao.coreport.CoreportDao;
import com.example.demo.dao.coreport.CoreportReplyDao;
import com.example.demo.dao.coreport.CoreportWorkerDao;
import com.example.demo.dao.report.ReportCoreportDao;
import com.example.demo.service.bug.BugService;
import com.example.demo.service.coreport.CoreportService;
import com.example.demo.service.picture.PictureService;
import com.example.demo.service.user.UserService;
import com.example.demo.utils.Constant;
import com.example.demo.vo.ResultVO;
import com.example.demo.vo.bug.BugInfoVO;
import com.example.demo.vo.bug.BugVO;
import com.example.demo.vo.coreport.CoreportPreVO;
import com.example.demo.vo.coreport.CoreportInfoVO;
import com.example.demo.vo.coreport.CoreportReplyVO;
import com.example.demo.vo.coreport.CoreportVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CoreportServiceImpl implements CoreportService {
    @Resource
    CoreportDao coreportDao;
    @Resource
    CoreportBugDao coreportBugDao;
    @Resource
    CoreportReplyDao coreportReplyDao;
    @Resource
    CoreportWorkerDao coreportWorkerDao;
    @Resource
    ReportCoreportDao reportCoreportDao;
    @Resource
    BugService bugService;
    @Resource
    PictureService pictureService;
    @Resource
    UserService userService;


    @Override
    public ResultVO<CoreportInfoVO> submitCoreport(CoreportInfoVO coreportInfoVO) {
        CoreportVO coreportVO=new CoreportVO(coreportInfoVO);
        coreportVO.setNowtime(LocalDateTime.now());
        coreportDao.insertCoreprt(coreportVO);
        int coid=coreportVO.getCoid();
        reportCoreportDao.insertReportCoreport(coreportInfoVO.getRid(),coid);
        coreportReplyDao.insertCoreportReplyDao(coid,coreportInfoVO.getReply());
        coreportWorkerDao.insertCoreportWorker(coid,coreportInfoVO.getUid());
        for(BugInfoVO bugInfoVO:coreportInfoVO.getBugList()){
            BugVO bugVO=new BugVO(bugInfoVO);
            bugService.insertBug(bugVO);
            int bid=bugVO.getBid();
            coreportBugDao.insertCoreportBug(coid,bid);
            List<String> pictures=bugInfoVO.getPictures();
            for(String picture:pictures){
                int pid=pictureService.getPictureByAddress(picture).getPid();
                pictureService.insertBugPicture(bid,pid);
            }
        }
        return new ResultVO<>(1,"成功",coreportInfoVO);
    }

    @Override
    public ResultVO<List<CoreportReplyVO>> getCoreportListByRid(int rid,int pageNum) {
        List<CoreportReplyVO> coreportReplyVOList=new ArrayList<>();
        List<Integer> coidList=reportCoreportDao.selectCoidByRid(rid,pageNum, Constant.DEFAULT_PAGE_SIZE);
        if(coidList.size()==0){
            return new ResultVO<>(-1,"未查询到协作报告",new ArrayList<CoreportReplyVO>());
        }
        for(int coid:coidList){
            coreportReplyVOList.add(coreportReplyDao.selectReplyByCoid(coid));
        }
        int fullPageNum=(reportCoreportDao.selectCoidByRid(rid,1, Constant.NO_PAGE_SIZE).size()-1)/10+1;
        return new ResultVO<>(1,pageNum+"/"+fullPageNum,coreportReplyVOList);
    }

    //获得协作报告
    @Override
    public ResultVO<CoreportPreVO> getCoreportByCoid(int coid){
        CoreportPreVO res = new CoreportPreVO();
        List<BugInfoVO> bugList = new ArrayList<BugInfoVO>();
        bugList=bugService.getInfoByCoreport(coid).getData();
        res.setBugList(bugList);
        if(coreportDao.selectTimeByCoid(coid)==null){
            return(new ResultVO<CoreportPreVO>(-1,"未找到报告",new CoreportPreVO()));
        }
        res.setNowtime(coreportDao.selectTimeByCoid(coid));
        res.setReply(coreportReplyDao.selectReplyByCoid(coid).getReply());
        res.setUsername(userService.getNameByUid(coreportWorkerDao.selectUidByCoid(coid)));
        return new ResultVO<CoreportPreVO>(1,"查询成功",res);
    }

    @Override
    public int getRecentCoreportNumByUser(String username) {
        return coreportDao.selectRecentCoreportNumByUser(username);
    }
}
